package iosr11.gbase;

import iosr11.gcontacts.Contacts;
import iosr11.gcontacts.ContactsImpl;

import java.net.URL;

import com.google.api.gbase.client.FeedURLFactory;
import com.google.api.gbase.client.GoogleBaseEntry;
import com.google.api.gbase.client.GoogleBaseFeed;
import com.google.api.gbase.client.GoogleBaseQuery;
import com.google.api.gbase.client.GoogleBaseService;

public class BaseEngineImpl implements BaseDAO{

	private Contacts contacts;
	private StudentBase studentBase;
	private TeacherBase teacherBase;
	private MarkDAO marksBase;
	
	@Override
	public StudentBase getStudentBase() {
		return this.studentBase;
	}

	@Override
	public TeacherBase getTeacherBase() {
		return this.teacherBase;
	}

	@Override
	public MarkDAO getMarksBase() {
		return this.marksBase;
	}

	private GoogleBaseService service;
	private static BaseEngineImpl singleton;
	
	private static String TITLE = "iosr11.base";
	
	private BaseEngineImpl(){
		contacts = new ContactsImpl();
		service = new GoogleBaseService(TITLE);
		studentBase = new StudentBaseImpl(this, contacts);
		teacherBase = new TeacherBaseImpl(this, contacts);
		marksBase = new StudentMarksBaseImpl(this);
	}
	
	public static BaseEngineImpl getInstance(){
		if (singleton==null){
			singleton = new BaseEngineImpl();
		}
		return singleton;
	}
	
	@Override
	public boolean addRecord(GoogleBaseEntry entry) {
		if (this.addRecordReturnId(entry) != null)
			return true;
		else
			return false;
	}

	@Override
	public String addRecordReturnId(GoogleBaseEntry entry) {
		try {
			GoogleBaseEntry afterAdd = service.insert(FeedURLFactory.getDefault().getItemsFeedURL(), entry);
			return afterAdd.getId();
		}
		catch (Exception e){
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public boolean authtenticate(String username, String password) {
		try {
			service.setUserCredentials(username, password);
			return contacts.autenticate(username, password);
		}
		catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public GoogleBaseEntry getRecord(String fieldName, String value) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public GoogleBaseEntry getRecord(URL ref) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public GoogleBaseService getService() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public GoogleBaseFeed getQueryResult(GoogleBaseQuery query) {
		try {
			return service.query(query);
		}
		catch (Exception e){
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public GoogleBaseFeed getQueryResult(GoogleBaseQuery query, String key,
			int value) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public GoogleBaseFeed getQueryResult(GoogleBaseQuery query, String key,
			String value) {
		GoogleBaseFeed queryResult = this.getQueryResult(query);
		if (queryResult == null)
			return null;
		
		GoogleBaseFeed result = new GoogleBaseFeed();
		for (GoogleBaseEntry entry : queryResult.getEntries()){
			if (entry.getGoogleBaseAttributes().getTextAttribute(key)!=null)
				if (entry.getGoogleBaseAttributes().getTextAttribute(key).equals(value))
					result.addEntry(entry);
		}
		if (result.getEntries().size()<1) return null;
		return result;
	}
	
}
